﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.SystemManage
{
    public partial class HospitalList : BasePage
    {

        IBLL_Hospital ih = Factory.BLL_Hospital();
        IBLL_User iu = Factory.BLL_User();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();

                //设置下拉框Enable
                GetDefaultAreaString();

            }
        }

        /// <summary>
        /// 绑定所有医疗机构
        /// </summary>
        private void SetBind()
        {
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount;
            Model.Query.Hospital qHospital = new HZ.DrugTrade.Model.Query.Hospital();
            qHospital.HospitalName = txtHospitalName.Text.Trim();
            //if (!string.IsNullOrEmpty(this.ddlJICeng.SelectedValue))
            //{
            //    if (this.ddlJICeng.SelectedValue == "1")//只查基层医疗机构
            //    {
            //        qHospital.Append = "HospitalId>'H0754' and HospitalId<'H7000'";
            //    }
            //    else//只查基层以上医疗机构
            //    {
            //        qHospital.Append = "HospitalId<='H0754' or HospitalId>='H7000'";
            //    }
            //}
            qHospital.AreaID = GetAreaID();
            IList<Hospital> lstHospital = ih.GetHospitalByWhere(qHospital, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwHospital.DataSource = lstHospital;
            gvwHospital.DataBind();
        }

        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";
            //防止areaId为null值
            try
            {
                //首次进入使用当前用户所在区域ID
                areaId = CurrentAccount.GetUser().Org.Area.AreaID;

                //优先获取区县，其次城市，再次省份
                if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"];
                }
                else
                {
                    if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                    {
                        areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
                    }
                    else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                    {
                        areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
                    }

                }
            }
            catch (Exception ex)
            {
                return areaId;
            }
            return areaId;
        }
        protected string GetTelNoById(string hosID)
        {
            object hospital = HZ.Data.DbHelperFactory.Create().ExecuteScalar("select TelNo from tb_hospitalInfo where hospitalId='" + hosID + "'");
            if (hospital != null)
            {
                return hospital.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "320000";
            string defaultCity = "";
            //
            string strArea = "";

            //获取当前用户所在区域
            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(Constants.CURRENT_DEPLOY_AREAID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            //显示默认地区，范围之外的内容禁止选择
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            SetBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetBind();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            Model.Query.Hospital qHospital = new HZ.DrugTrade.Model.Query.Hospital();
            qHospital.HospitalName = txtHospitalName.Text.Trim();
            qHospital.AreaID = GetAreaID();
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.Hospital>(qHospital, true);
            string sql = string.Format(@"select hospitalname 医疗机构名称,HospitalAddress 地址,ContactPerson 联系人,contacttel 联系人电话 from View_HospitalArea where 1=1 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "医疗机构信息.xls");
        }

      
        
    }
    }